<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/10/12
 * Time: 13:16
 */

namespace app\index\controller;

use app\model\GroupChat as GroupChatModel;
use app\model\GroupChatList as GroupChatListModel;
use app\validate\GroupChatValidate;

class GroupChat
{
    /**
     * 获取我创建的群聊
     * @return false|string
     */
    public function getMyJoinGroupChat(){
        if(request()->isPost()){
            $user_id = getNowUserId();
            $GroupChatModel = new GroupChatModel();
            $groupChatListModel = new GroupChatListModel();
            $myGroupChat = $groupChatListModel->getUserChatList($user_id);
            return res(1,'',$myGroupChat);
        }
        return res(0,MSG_NOT_POST);
    }
    public function getMyCreateGroupChat(){
        if(request()->isPost()){
            $user_id = getNowUserId();
            $groupChatModel = new GroupChatModel();
            return res(1,'',$groupChatModel->getMyCreateGroupChat($user_id));
        }
        return res(0,MSG_NOT_POST);
    }
    /**
     * 获取群聊好友列表
     * @param {post} group_chat_id 群聊编号
     * @return false|string
     */
    public function getGroupUserList(){
        if(request()->isPost()){
            $user_id = getNowUserId();
            $group_id = input('post.group_chat_id');
            $groupChatModel = new GroupChatModel();
            if($groupChatModel->check($group_id) == 0) return res(0,'群聊不存在');
            if(!checkNull($group_id)) return res(0,MSG_NOT_KEY);
            $groupChatListModel = new GroupChatListModel();
            if($groupChatListModel->checkByUser($user_id,$group_id) == 0) return res(0,'你不是本群成员，无权限查看');
            $groupChatUserList = $groupChatListModel->getGroupChatUserList($group_id);
            return res(1,'',$groupChatUserList);
        }
        return res(0,MSG_NOT_POST);
    }
    /**
     * 创建群聊
     * @param {post} group_max 群聊编号
     * @param {post} group_chat_name 群聊名称
     * @return false|string
     * @throws \think\exception\PDOException
     */
    public function createGroupChat(){
        if(request()->isPost()){
            $user_id = getNowUserId();      //获取当前登录用户
            $data = request()->only("group_max,group_chat_name");
            $GroupChatValidate = new GroupChatValidate();
            //验证前端传来的字段
            if(!$GroupChatValidate->scene('createGroup')->check($data)) return res(0,$GroupChatValidate->getError());
            //填充固定数据
            $data['status'] = 1;
            $data['group_num'] = 1;
            $data["user_id"] = $user_id;
            $data["create_time"] = time();
            $GroupChat = new GroupChatModel();
            //开启事务
            $GroupChat->startTrans();
            //创建群聊
            $group_chat = $GroupChat->add($data);
            if(!$group_chat){
                //创建失败，数据回滚
                $GroupChat->rollback();
                return res(0,'创建失败');
            }
            //获取刚刚创建的群聊编号
            $group_chat_id = $group_chat['group_chat_id'];
            //群主信息
            $group_first_user = [
                'group_chat_id' => $group_chat_id,
                'group_chat_level' => 10,
                'user_id' => $user_id,
                'user_status' => 1,
                'join_time' => time()
            ];
            $GroupChatListModel = new GroupChatListModel();
            //在群聊列表中添加本用户
            $set_first_user = $GroupChatListModel->add($group_first_user);
            if($set_first_user){
                //添加成功，数据提交
                $GroupChat->commit();
                return res(1,'创建成功');
            }
            //添加失败，数据回滚
            $GroupChat->rollback();
            return res(0,'创建失败');
        }
        return res(0,MSG_NOT_POST);
    }

    /**
     * 加入群聊
     * @param {post} group_chat_id 群聊编号
     * @return false|string
     */
    public function joinGroupChat(){
        if(request()->isPost()){
            $user_id = getNowUserId();
            $group_chat_id = input('post.group_chat_id');
            if(!checkNull($group_chat_id)) return res(0,MSG_NOT_KEY);
            $groupChatModel = new GroupChatModel();
            if($groupChatModel->check($group_chat_id) == 0) return res(0,'群聊不存在');
            $group_chat = $groupChatModel->getById($group_chat_id);
            if($group_chat["group_num"] == $group_chat["group_max"]) return res(0,'群聊人数已满');
            $groupChatListModel = new GroupChatListModel();
            if($groupChatListModel->checkByUser($user_id,$group_chat_id) != 0) return res(0,'你已在该群聊当中');
            $data = [
                'user_id' => $user_id,
                'group_chat_id' => $group_chat_id,
                'user_status' => 1,
                'group_chat_level' => 1,
                'join_time' => time()
            ];
            $groupChatModel->startTrans();
            if(!$groupChatListModel->add($data)){
                $groupChatModel->rollback();
                return res(0,'加入群聊失败');
            }
            if($groupChatModel->setNumIns($group_chat_id)){
                $groupChatModel->commit();
                return  res(0,'加入群聊成功');
            }
            $groupChatModel->rollback();
            return res(0,'加入群聊失败');
        }
        return res(0,'',MSG_NOT_POST);
    }

    /**
     * 解散群聊
     * @param {post} group_chat_id 群聊编号
     * @return false|string
     * @throws \think\exception\PDOException
     */
    public function dismissGroupChat(){
        if(request()->isPost()){
            $user_id = getNowUserId();
            $group_chat_id = input('post.group_chat_id');
            if(!checkNull($group_chat_id)) return res(0,MSG_NOT_KEY);
            $groupChatModel = new GroupChatModel();
            if($groupChatModel->checkMy($group_chat_id,$user_id) == 0) return res(0,'您不是群主，无法解散');
            $groupChatModel->startTrans();
            if(!$groupChatModel->del($group_chat_id)){
                $groupChatModel->rollback();
                return res(0,'解散失败');
            }
            $groupChatListModel = new GroupChatListModel();
            $dismissUser = $groupChatListModel->disGroupChat($group_chat_id);
            if($dismissUser){
                $groupChatModel->commit();
                return res(1,'群聊解散成功');
            }
            $groupChatModel->rollback();
            return res(0,'解散失败');

        }
        return res(0,MSG_NOT_POST);
    }

    /**
     * 将用户移除群聊
     * @param {post} group_chat_id 群聊编号
     * @param {post} user_id 将要移除群聊的用户编号
     * @return false|string
     */
    public function moveUser(){
        if(request()->isPost()){
            $user_id = getNowUserId();      //获取当前登录用户
            $group_chat_id = input('post.group_chat_id');
            $target = input('post.user_id');
            if (!checkNull($group_chat_id) || !checkNull($target)) return  res(0,MSG_NOT_KEY);
            $groupChatModel = new GroupChatModel();
            //核查是否有权限
            if($groupChatModel->checkMy($group_chat_id,$user_id) == 0)return  res(0,'您无权限进行移除操作');
            $groupChatListModel = new GroupChatListModel();
            //判断该群聊是否存在该用户
            if($groupChatListModel->checkByUser($target,$group_chat_id) == 0)return  res(0,'该用户不在本群');
            $groupChatListModel->startTrans();
            $moveUser = $groupChatListModel->moveUser($target,$group_chat_id);
            if(!$moveUser){
                $groupChatListModel->rollback();
                return  res(0,'移除失败');
            }
            $groupChatDec = $groupChatModel->setNumDec($group_chat_id);
            if($groupChatDec){
                $groupChatListModel->commit();
                return res(1,'移除成功');
            }
            $groupChatListModel->rollback();
            return  res(0,'移除失败');

        }
        return res(0,MSG_NOT_POST);
    }

    /**
     * 升级群规模
     * @param {post} group_chat_id 群聊编号
     * @param {post} group_max 群规模
     * @return false|string
     */
    public function upgradeGroupChat(){
        if(request()->isPost()){
            $user_id = getNowUserId();      //获取当前登录用户
            $group_chat_id = input('post.group_chat_id');
            $group_max = input('post.group_max');
            if(!checkNull($group_chat_id) || !checkNull($group_max)) return res(0,MSG_NOT_KEY);
            $groupChatModel = new GroupChatModel();
            $groupChat = $groupChatModel->getById($group_chat_id);
            //核查是否为群主
            if($user_id != $groupChat['user_id']) return res(0,'您不是群主，无权限进行操作');
            //核查群规模
            if($group_max <= $groupChat['group_max'])  return res(0,'群规模已达' . $groupChat['group_max'] . '人群,无需升级');
            $GroupChatValidate = new GroupChatValidate();
            $data['group_max'] = $group_max;
            //验证
            if(!$GroupChatValidate->scene('upgrade')->check($data)) return res(0,$GroupChatValidate->getError());
            $res = $groupChatModel->edit($group_chat_id,$data);
            if($res) return res(1,'升级成功');
            return res(0,'升级失败');

        }
        return res(0,MSG_NOT_POST);
    }
}